package com.zlj.accountant.core;

import java.util.Arrays;

public class MethodHandlerKey {
	
	public static class ResultMethodProviderKey extends MethodHandlerKey{
		public Class<?> returnClass;
		public Class<?>[] paramClases;
		public ResultMethodProviderKey(Class<?> returnClass,Class<?>... paramClases){
			this.paramClases = paramClases;
			this.returnClass = returnClass;
        }
		@Override
		public boolean equals(Object o) {
			if(o == null)return false;
			if(!(o instanceof ResultMethodProviderKey))return false;
			ResultMethodProviderKey target = getClass().cast(o);
			return this.toString().equals(target.toString());
		}
		
		@Override
		public String toString() {
			StringBuilder buf = new StringBuilder();
			buf.append("[r:["+getClassName(returnClass)+"];p:"+MethodHandlerKey.getClassName(paramClases)+"]");
			return buf.toString();
		}
				
	}
	
	public static class TaskMethodHandlerKey extends MethodHandlerKey {
		
		private Class<?> taskClass;
		@SuppressWarnings("rawtypes")
        private Enum e;
		private Class<?> ec;
		
		public TaskMethodHandlerKey(@SuppressWarnings("rawtypes") Enum e,Class<?> taskClass){
			this.taskClass = taskClass;
			this.e = e;
			this.ec = e.getClass();
        }
		
		
		@Override
		public boolean equals(Object o) {
			if(o == null)return false;
			if(!(o instanceof TaskMethodHandlerKey))return false;
			TaskMethodHandlerKey target = getClass().cast(o);
			return this.toString().equals(target.toString());
		}
		
		@Override
		public String toString() {
			StringBuilder buf = new StringBuilder();
			buf.append("[t:["+getClassName(taskClass)+"];ec:["+getClassName(ec)+"];m:"+e+"]");
			return buf.toString();
		}
		

	}
	
	public static class ResultListMethodHandlerKey extends ResultMethodProviderKey{

		public ResultListMethodHandlerKey(Class<?> returnClass,
                Class<?>... paramClases)
        {
	        super(returnClass, paramClases);
        }
		@Override
		public boolean equals(Object o) {
			if(o == null)return false;
			if(!(o instanceof ResultListMethodHandlerKey))return false;
			ResultListMethodHandlerKey target = getClass().cast(o);
			return this.toString().equals(target.toString());
		}

	}
	
	public static class EventMethodHandlerKey extends MethodHandlerKey{
		
		private Class<Event> eventClass;
		
		public EventMethodHandlerKey(Class<Event> eventClass){
			this.eventClass = eventClass;
        }
		
		@Override
		public boolean equals(Object o) {
			if(o == null)return false;
			if(!(o instanceof EventMethodHandlerKey))return false;
			EventMethodHandlerKey target = getClass().cast(o);
			return this.toString().equals(target.toString());
		}
		
		@Override
		public String toString() {
			StringBuilder buf = new StringBuilder();
			buf.append("[e:["+getClassName(eventClass)+"]]");
			return buf.toString();
		}

	}
	
	private static String getClassName(Class<?> clazs){
		return clazs.getName().replaceAll("[\\$][0-9]+", "");
	}
	
	private static String getClassName(Class<?>[] clazses){
		String[] r = new String[clazses.length];
		for (int i = 0; i < r.length; i++) {
			r[i] = getClassName(clazses[i]);
		}
		return Arrays.toString(r);
	}
		
	@Override
	public int hashCode() {
		return toString().hashCode();
	}

}
